﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Net.WebSockets;
using WebSockets.modules;

namespace WebSockets
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private WebSocketServer _webSocketServer;

        public MainWindow()
        {
            InitializeComponent();

            _webSocketServer = new WebSocketServer();
            _webSocketServer.Start();

            _webSocketServer.WebSocketMessageReceived += _webSocketServer_WebSocketMessageReceived;
        }

        private void _webSocketServer_WebSocketMessageReceived(object sender, WebSocketMessageEventArgs e)
        {
            reciveText.Text = e.Message;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach(var w in _webSocketServer.WebSocketList)
            {
                _webSocketServer.SendAsync(w, $"wpf hello. {DateTime.Now.ToLocalTime()}");
            }
        }
    }
}
